unit fuQExport4ODTEditor;

{$I QExport4VerCtrl.inc}

interface

uses
  Windows, Messages, SysUtils{$IFDEF VCL6}, Variants{$ENDIF}, Classes, Graphics,
  Controls, Forms, Dialogs, ExtCtrls, StdCtrls, Buttons, ComCtrls, ToolWin,
  QExport4BaseODFClass {$IFDEF VCL4}, ImgList{$ENDIF}, Menus, QExport4, QExport4Common,
  QExport4ODT, QExport4StrIds;

type
  TfmQExport4ODTEditor = class(TForm)
    paButtons: TPanel;
    bOK: TButton;
    bCancel: TButton;
    paFile: TPanel;
    laFileName: TLabel;
    edFileName: TEdit;
    sbSave: TSpeedButton;
    cbShow: TCheckBox;
    cbPrint: TCheckBox;
    paOptions: TPanel;
    paSelector: TPanel;
    paTableName: TPanel;
    laTableName: TLabel;
    edTableName: TEdit;
    pcStyleEditor: TPageControl;
    tsOptions: TTabSheet;
    tsStyles: TTabSheet;
    tbProcessing: TToolBar;
    ilButtons: TImageList;
    ilListView: TImageList;
    bAdd: TToolButton;
    bRemove: TToolButton;
    bSeparator1: TToolButton;
    bMoveUp: TToolButton;
    bMoveDown: TToolButton;
    pcOptions: TPageControl;
    tsCommon: TTabSheet;
    tsHeader: TTabSheet;
    tsFooter: TTabSheet;
    tsFormats: TTabSheet;
    mmHeader: TMemo;
    mmFooter: TMemo;
    laInteger: TLabel;
    laDate: TLabel;
    laDateTime: TLabel;
    laTrue: TLabel;
    laNull: TLabel;
    edInteger: TEdit;
    edDateTime: TEdit;
    edDate: TEdit;
    edTrue: TEdit;
    edNull: TEdit;
    laFalse: TLabel;
    edFalse: TEdit;
    laFloat: TLabel;
    edFloat: TEdit;
    laTime: TLabel;
    edTime: TEdit;
    laCurrency: TLabel;
    edCurrency: TEdit;
    cbAllowCaptions: TCheckBox;
    cbAutoCalcStrType: TCheckBox;
    cbVisibleFields: TCheckBox;
    cbGoToFirsrRec: TCheckBox;
    bvl1: TBevel;
    bvl2: TBevel;
    laSkipRecCount_01: TLabel;
    edSkipRecCount: TEdit;
    laSkipRecCount_02: TLabel;
    edCaptionRow: TEdit;
    laCaptionRow: TLabel;
    rbExportAll: TRadioButton;
    rbExportOnly: TRadioButton;
    edExportRecCount: TEdit;
    laExportRecCount_02: TLabel;
    lvOptions: TListView;
    rgStripStyle: TRadioGroup;
    lvSStyles: TListView;
    paStylesEdit: TPanel;
    pcAllOptions: TPageControl;
    tsCommonCellStyle: TTabSheet;
    tsCommonTextStyle: TTabSheet;
    pcCommonTextStyle: TPageControl;
    tsTextFont: TTabSheet;
    pcCommonCellStyle: TPageControl;
    tsCellFont: TTabSheet;
    laCellFont: TLabel;
    cbCellFont: TComboBox;
    cbCellFontSize: TComboBox;
    laCellFontSize: TLabel;
    btnCellColor: TSpeedButton;
    pbCellFontColor: TPaintBox;
    Bevel5: TBevel;
    btnCellBold: TSpeedButton;
    btnCellItalic: TSpeedButton;
    btnCellStrikeOut: TSpeedButton;
    Bevel6: TBevel;
    btnCellUnderlineSingle: TSpeedButton;
    Bevel4: TBevel;
    btnCellLeft: TSpeedButton;
    btnCellCenter: TSpeedButton;
    btnCellRight: TSpeedButton;
    btnCellJustify: TSpeedButton;
    Bevel8: TBevel;
    Bevel3: TBevel;
    Bevel11: TBevel;
    btnCellVerTop: TSpeedButton;
    btnCellVerCenter: TSpeedButton;
    btnCellVerBottom: TSpeedButton;
    laTextFont: TLabel;
    cbTextFont: TComboBox;
    cbTextFontSize: TComboBox;
    laTextSize: TLabel;
    btnTextFontColor: TSpeedButton;
    pbTextFontColor: TPaintBox;
    Bevel1: TBevel;
    btnTextBold: TSpeedButton;
    btnTextItalic: TSpeedButton;
    btnTextStrikeOut: TSpeedButton;
    Bevel2: TBevel;
    btnTextUnderLine: TSpeedButton;
    Bevel7: TBevel;
    btnTextLeft: TSpeedButton;
    btnTextCenter: TSpeedButton;
    btnTextRight: TSpeedButton;
    btnTextJustify: TSpeedButton;
    Bevel10: TBevel;
    Bevel12: TBevel;
    ColorDialog: TColorDialog;
    sdFileName: TSaveDialog;
    pmHeaderFooter: TPopupMenu;
    miInsertTab: TMenuItem;
    miClear: TMenuItem;
    bbResetDefault: TBitBtn;
    bbRestoreInitial: TBitBtn;
    tsBorderStyle: TTabSheet;
    edBorderWidth: TEdit;
    cbUseBorder: TCheckBox;
    laBorderWidth: TLabel;
    Bevel9: TBevel;
    laBorderColor: TLabel;
    cbTextBackground: TCheckBox;
    sbTextBackgroundColor: TSpeedButton;
    pbBackColor: TPaintBox;
    cbTextUseHighlight: TCheckBox;
    sbTextHighlightColor: TSpeedButton;
    pbTextHighlight: TPaintBox;
    cbCellUseHighlight: TCheckBox;
    cbCellUseBackground: TCheckBox;
    sbCellHighlight: TSpeedButton;
    pbCellHighlight: TPaintBox;
    sbCellBackground: TSpeedButton;
    pbCellBackground: TPaintBox;
    btnBorderTopColor: TSpeedButton;
    pbBorderTop: TPaintBox;
    paSampleText: TPanel;
    laSampleText: TLabel;
    pbSampleText: TPaintBox;
    bResetAll: TBitBtn;
    bResetItem: TBitBtn;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure bbRestoreInitialClick(Sender: TObject);
    procedure bbResetDefaultClick(Sender: TObject);
    procedure miInsertTabClick(Sender: TObject);
    procedure miClearClick(Sender: TObject);
    procedure rbExportAllClick(Sender: TObject);
    procedure rbExportOnlyClick(Sender: TObject);
    procedure sbSaveClick(Sender: TObject);
    procedure pbBorderTopPaint(Sender: TObject);
    procedure btnBorderTopColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBorderTopColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnBorderTopColorClick(Sender: TObject);
    procedure cbUseBorderClick(Sender: TObject);
    procedure edBorderWidthChange(Sender: TObject);
    procedure edBorderWidthEnter(Sender: TObject);
    procedure edBorderWidthExit(Sender: TObject);
    procedure bAddClick(Sender: TObject);
    procedure lvSStylesChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure bRemoveClick(Sender: TObject);
    procedure bMoveUpClick(Sender: TObject);
    procedure bMoveDownClick(Sender: TObject);
    procedure lvOptionsChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure pcStyleEditorChange(Sender: TObject);
    procedure cbCellUseBackgroundClick(Sender: TObject);
    procedure cbCellUseHighlightClick(Sender: TObject);
    procedure sbCellBackgroundClick(Sender: TObject);
    procedure sbCellBackgroundMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure sbCellBackgroundMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pbCellBackgroundPaint(Sender: TObject);
    procedure sbCellHighlightClick(Sender: TObject);
    procedure sbCellHighlightMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure sbCellHighlightMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pbCellHighlightPaint(Sender: TObject);
    procedure cbTextBackgroundClick(Sender: TObject);
    procedure sbTextBackgroundColorClick(Sender: TObject);
    procedure sbTextBackgroundColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure sbTextBackgroundColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pbBackColorPaint(Sender: TObject);
    procedure cbTextUseHighlightClick(Sender: TObject);
    procedure sbTextHighlightColorClick(Sender: TObject);
    procedure sbTextHighlightColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure sbTextHighlightColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pbTextHighlightPaint(Sender: TObject);
    procedure btnCellColorClick(Sender: TObject);
    procedure btnCellColorMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure btnCellColorMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure pbCellFontColorPaint(Sender: TObject);
    procedure btnTextFontColorClick(Sender: TObject);
    procedure btnTextFontColorMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure btnTextFontColorMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure pbTextFontColorPaint(Sender: TObject);
    procedure btnTextBoldClick(Sender: TObject);
    procedure btnTextItalicClick(Sender: TObject);
    procedure btnTextStrikeOutClick(Sender: TObject);
    procedure btnTextUnderLineClick(Sender: TObject);
    procedure btnCellBoldClick(Sender: TObject);
    procedure btnCellItalicClick(Sender: TObject);
    procedure btnCellStrikeOutClick(Sender: TObject);
    procedure btnCellUnderlineSingleClick(Sender: TObject);
    procedure btnCellLeftClick(Sender: TObject);
    procedure btnCellCenterClick(Sender: TObject);
    procedure btnCellRightClick(Sender: TObject);
    procedure btnCellJustifyClick(Sender: TObject);
    procedure btnCellVerTopClick(Sender: TObject);
    procedure btnCellVerCenterClick(Sender: TObject);
    procedure btnCellVerBottomClick(Sender: TObject);
    procedure btnTextLeftClick(Sender: TObject);
    procedure btnTextCenterClick(Sender: TObject);
    procedure btnTextRightClick(Sender: TObject);
    procedure btnTextJustifyClick(Sender: TObject);
    procedure NumberKeyPress(Sender: TObject; var Key: Char);
    procedure pbSampleTextPaint(Sender: TObject);
    procedure cbTextFontChange(Sender: TObject);
    procedure cbTextFontSizeChange(Sender: TObject);
    procedure cbTextFontSizeEnter(Sender: TObject);
    procedure cbTextFontSizeExit(Sender: TObject);
    procedure cbCellFontChange(Sender: TObject);
    procedure cbCellFontSizeChange(Sender: TObject);
    procedure cbCellFontSizeEnter(Sender: TObject);
    procedure cbCellFontSizeExit(Sender: TObject);
    procedure bResetItemClick(Sender: TObject);
    procedure bResetAllClick(Sender: TObject);
  private
    FExportODT: TQExport4ODT;
    FBorder: TODFBorder;
    FHrStyle: TODTParagraphStyle;
    FFrStyle: TODTParagraphStyle;
    FCRStyle: TODTCellParagraphStyle;
    FDStyle: TODTCellParagraphStyle;
    FCurrFormats: TQExportFormats;
    FNeedResetFormats: Boolean;
    FSStylesList: TODTStylesList;
    FCurrTextStyle: TODTParagraphStyle;
    FCurrCellStyle: TODTCellParagraphStyle;
    FCurrState: Integer;

    function CurrentSheetMemo: TMemo;

    procedure AddMemos;
    procedure AddListItem(Dest: TListView; ItemName: string; ImageInd: Integer);
    procedure ApplyChanges;
    procedure BlockUnBlock(Condition: Boolean);
    procedure ClearMemos;
    procedure EnableSkipRecGroup(Value: Integer);
    procedure FillCommonFields;
    procedure FillStripType;
    procedure FillStripStyles;
    procedure FillFormats;
    procedure FormOptions;
    procedure GetCurrentStyle;
    procedure ReplaceHeaderFooter;
    procedure SetSelectedToFocused(Item: TListItem);
    procedure SetCurrentStatus;
    procedure StyleRenumber(Item: TListItem);
    procedure TuneBackgroundControls(Condition: Boolean);
    procedure TuneBorderControls;
    procedure TuneExportGroup;
    procedure TuneHighlightControls(Condition: Boolean);
    procedure TuneSStyleButtons;
    procedure TuneOptionsList;
  public
    constructor Create(AOwner: TComponent); override;

    property ExportField: TQExport4ODT read FExportODT write FExportODT;
    property CurrentFormats: TQExportFormats read FCurrFormats
      write FCurrFormats;
    property SStylesList: TODTStylesList read FSStylesList write FSStylesList;
    property NeedResetFormats: Boolean read FNeedResetFormats;
    property Border: TODFBorder read FBorder write FBorder;
    property HrStyle: TODTParagraphStyle read FHrStyle write FHrStyle;
    property FrStyle: TODTParagraphStyle read FFrStyle write FFrStyle;
    property CRStyle: TODTCellParagraphStyle read FCRStyle write FCRStyle;
    property DStyle: TODTCellParagraphStyle read FDStyle write FDStyle;
  end;

function RunQExportODTEditor(ExportODT: TQExport4ODT): boolean;

implementation

{$R *.dfm}

function RunQExportODTEditor(ExportODT: TQExport4ODT): boolean;
begin

  if not Assigned(ExportODT) then
    raise Exception.Create('The QExportODT parameter is not assigned!');

  with TfmQExport4ODTEditor.Create(nil) do
  try
    FExportODT := ExportODT;
    FCurrFormats := TQExportFormats.Create;
    FCurrFormats.Assign(ExportODT.Formats);
    FBorder := TODFBorder.Create;
    FBorder.Assign(ExportODT.ODTOptions.Border);
    FSStylesList := TODTStylesList.Create(nil);
    FSStylesList.Assign(ExportField.ODTOptions.StripStylesList);
    FHrStyle := TODTParagraphStyle.Create(nil);
    FFrStyle := TODTParagraphStyle.Create(nil);
    FCRStyle := TODTCellParagraphStyle.Create(nil);
    FDStyle := TODTCellParagraphStyle.Create(nil);
    FHrStyle.Assign(FExportODT.ODTOptions.HeaderStyle);
    FFrStyle.Assign(FExportODT.ODTOptions.FooterStyle);
    FCRStyle.Assign(FExportODT.ODTOptions.CaptionRowStyle);
    FDStyle.Assign(FExportODT.ODTOptions.DataStyle);

    pcStyleEditor.ActivePage := tsOptions;

    FNeedResetFormats := false;

    ClearMemos;
    AddMemos;
    FillCommonFields;
    FillFormats;
    FillStripType;
    FillStripStyles;
    TuneSStyleButtons;
    EnableSkipRecGroup(ExportODT.ExportRecCount);
    GetCurrentStyle;

    Result := ShowModal = mrOk;
    if Result then
      ApplyChanges;
  finally
    FHrStyle.Free;
    FFrStyle.Free;
    FCRStyle.Free;
    FDStyle.Free;
    FSStylesList.Free;
    FBorder.Free;
    Free;
  end;
end;


{ TfmQExport4ODTEditor }

procedure TfmQExport4ODTEditor.ApplyChanges;
begin
  //Re-assign modified styles
  with ExportField.ODTOptions do
  begin
    HeaderStyle.Assign(FHrStyle);
    FooterStyle.Assign(FFrStyle);
    CaptionRowStyle.Assign(FCRStyle);
    DataStyle.Assign(FDStyle);
    StripStylesList.Assign(SStylesList);
  end;
  //Replace old headers and footers
  ReplaceHeaderFooter;
  //Re-assign new border
  ExportField.ODTOptions.Border.Assign(Border);
  //Commit changes for Allow Caption property
  ExportField.AllowCaptions := cbAllowCaptions.Checked;
  //Commit changes for Auto Calculate String Type property
  ExportField.AutoCalcStrType := cbAutoCalcStrType.Checked;
  //Commit changes for Only Visible Fields Type property
  ExportField.OnlyVisibleFields := cbVisibleFields.Checked;
  //Commit changes for Go to first record property
  ExportField.GoToFirstRecord := cbGoToFirsrRec.Checked;
  //Commit changes for Table Name property
  if ExportField.TableName <> edTableName.Text then
    ExportField.TableName := edTableName.Text;
  //Commit changes for File Name property
  if (edFileName.Text <> ExportField.FileName) then
    ExportField.FileName := edFileName.Text;
  //Commit changes for Skip Record Count property
  if (StrToIntDef(edSkipRecCount.Text, 0) <> ExportField.SkipRecCount) then
    ExportField.SkipRecCount := StrToIntDef(edSkipRecCount.Text, 0);
  //Commit changes for Caption Row property
  if (StrToIntDef(edCaptionRow.Text, -1) <> ExportField.CaptionRow) then
    ExportField.CaptionRow := StrToIntDef(edCaptionRow.Text, -1);
  //Commit changes for Show File property
  ExportField.ShowFile := cbShow.Checked;
  //Commit changes for Print File property
  ExportField.PrintFile := cbPrint.Checked;
  //Fill Skip Rec Count
  if ExportField.ExportRecCount <> StrToIntDef(edExportRecCount.Text, 0) then
    ExportField.ExportRecCount := StrToIntDef(edExportRecCount.Text, 0);
  //Fill Current Style
  case rgStripStyle.ItemIndex of
    0: ExportField.ODTOptions.StripStyle := sstNone;
    1: ExportField.ODTOptions.StripStyle := sstColumn;
    2: ExportField.ODTOptions.StripStyle := sstRow;
  end;
  //Commit current formats
  with ExportField.Formats do
  begin
    if IntegerFormat <> edInteger.Text then
      IntegerFormat := edInteger.Text;
    if FloatFormat <> edFloat.Text then
      FloatFormat := edFloat.Text;
    if DateFormat <> edDate.Text then
      DateFormat := edDate.Text;
    if TimeFormat <> edTime.Text then
      TimeFormat := edTime.Text;
    if DateTimeFormat <> edDateTime.Text then
      DateTimeFormat := edDateTime.Text;
    if CurrencyFormat <> edCurrency.Text then
      CurrencyFormat := edCurrency.Text;
    if BooleanTrue <> edTrue.Text then
      BooleanTrue := edTrue.Text;
    if BooleanFalse <> edFalse.Text then
      BooleanFalse := edFalse.Text;
    if NullString <> edNull.Text then
      NullString := edNull.Text;
  end;
end;

procedure TfmQExport4ODTEditor.FillCommonFields;
begin
  edCaptionRow.Text := IntToStr(ExportField.CaptionRow);
  edSkipRecCount.Text := IntToStr(ExportField.SkipRecCount);
  edFileName.Text := ExportField.FileName;
  edTableName.Text := ExportField.TableName;
  cbAllowCaptions.Checked := ExportField.AllowCaptions;
  cbAutoCalcStrType.Checked := ExportField.AutoCalcStrType;
  cbVisibleFields.Checked := ExportField.OnlyVisibleFields;
  cbGoToFirsrRec.Checked := ExportField.GoToFirstRecord;
  cbShow.Checked := ExportField.ShowFile;
  cbPrint.Checked := ExportField.PrintFile;
  TuneBorderControls;
end;

procedure TfmQExport4ODTEditor.FillFormats;
begin
  if FNeedResetFormats then
  begin
    try
      FCurrFormats := TQExportFormats.Create;
      with FCurrFormats do
      begin
        edInteger.Text := IntegerFormat;
        edFloat.Text := FloatFormat;
        edDate.Text := DateFormat;
        edTime.Text := TimeFormat;
        edDateTime.Text := DateTimeFormat;
        edCurrency.Text := CurrencyFormat;
        edTrue.Text := BooleanTrue;
        edFalse.Text := BooleanFalse;
        edNull.Text := NullString;
      end;
    finally
      FCurrFormats.Free;
    end;
  end
  else
    with ExportField.Formats do
    begin
      edInteger.Text := IntegerFormat;
      edFloat.Text := FloatFormat;
      edDate.Text := DateFormat;
      edTime.Text := TimeFormat;
      edDateTime.Text := DateTimeFormat;
      edCurrency.Text := CurrencyFormat;
      edTrue.Text := BooleanTrue;
      edFalse.Text := BooleanFalse;
      edNull.Text := NullString;
    end;
  FNeedResetFormats := false;
end;

procedure TfmQExport4ODTEditor.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  FNeedResetFormats := false;
  for i := 0 to pcAllOptions.PageCount - 1 do
    pcAllOptions.Pages[i].Parent := paStylesEdit;
end;

procedure TfmQExport4ODTEditor.FormDestroy(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to pcAllOptions.PageCount - 1 do
    pcAllOptions.Pages[i].Parent := pcAllOptions;
end;

procedure TfmQExport4ODTEditor.bbRestoreInitialClick(Sender: TObject);
begin
  FNeedResetFormats := false;
  FillFormats;
end;

procedure TfmQExport4ODTEditor.bbResetDefaultClick(Sender: TObject);
begin
  FNeedResetFormats := true;
  FillFormats;
end;

function TfmQExport4ODTEditor.CurrentSheetMemo: TMemo;
begin
  if pcOptions.ActivePage = tsHeader then Result := mmHeader
  else if pcOptions.ActivePage = tsFooter then Result := mmFooter
  else Result := nil;
end;

procedure TfmQExport4ODTEditor.ClearMemos;
begin
  mmHeader.Lines.Clear;
  mmFooter.Lines.Clear;
end;

procedure TfmQExport4ODTEditor.AddMemos;
var
  i: Integer;
begin
  for i := 0 to ExportField.Header.Count - 1 do
    mmHeader.Lines.Add(ExportField.Header[i]);
  for i := 0 to ExportField.Footer.Count - 1 do
    mmFooter.Lines.Add(ExportField.Footer[i]);
end;

procedure TfmQExport4ODTEditor.miInsertTabClick(Sender: TObject);
var
  MM: TMemo;
  Point: TPoint;
  Str: string;
begin
  MM := CurrentSheetMemo;
  if Assigned(MM) then begin
    Point := GetMemoCaretPos(MM);
    Str := MM.Lines[Point.Y];
    Insert(Chr(9), Str, Point.X + 1);
    MM.Lines[Point.Y] := Str;
    MM.Perform(EM_SETSEL, Point.X + 1, Point.X + 1);
  end;
end;

procedure TfmQExport4ODTEditor.miClearClick(Sender: TObject);
var
  MM: TMemo;
begin
  MM := CurrentSheetMemo;
  if Assigned(MM) then MM.Lines.Clear;
end;

procedure TfmQExport4ODTEditor.TuneExportGroup;
begin
  edExportRecCount.Enabled := rbExportOnly.Checked;
  laExportRecCount_02.Enabled := rbExportOnly.Checked;
end;

procedure TfmQExport4ODTEditor.rbExportAllClick(Sender: TObject);
begin
  edExportRecCount.Text := '0';
  TuneExportGroup;
end;

procedure TfmQExport4ODTEditor.rbExportOnlyClick(Sender: TObject);
begin
  TuneExportGroup;
end;

procedure TfmQExport4ODTEditor.EnableSkipRecGroup(Value: Integer);
begin
  if (Value <> 0) then
  begin
    rbExportOnly.Checked := true;
    TuneExportGroup;
    edExportRecCount.Text := IntToStr(Value);
  end;
end;

procedure TfmQExport4ODTEditor.sbSaveClick(Sender: TObject);
begin
  sdFileName.FileName := edFileName.Text;
  if sdFileName.Execute then
    edFileName.Text := sdFileName.FileName;
end;

constructor TfmQExport4ODTEditor.Create(AOwner: TComponent);
begin
  inherited;
  cbCellFont.Items.Assign(Screen.Fonts);
  cbTextFont.Items.Assign(Screen.Fonts);
  FormOptions;
end;

procedure TfmQExport4ODTEditor.FillStripType;
begin
  rgStripStyle.ItemIndex := Integer(ExportField.ODTOptions.StripStyle);
end;

procedure TfmQExport4ODTEditor.pbBorderTopPaint(Sender: TObject);
begin
  if not Assigned(Border) then Exit;
  if Border.BorderStyle = bsODFNone then Exit;
  PaintStandardColors(pbBorderTop, Border.BorderColor);
end;

procedure TfmQExport4ODTEditor.btnBorderTopColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbBorderTop);
end;

procedure TfmQExport4ODTEditor.btnBorderTopColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbBorderTop);
end;

procedure TfmQExport4ODTEditor.btnBorderTopColorClick(Sender: TObject);
begin
  if not Assigned(Border) then Exit;
  if ColorDialog.Execute then
    if ColorDialog.Color <> -1 then
      Border.BorderColor := ColorDialog.Color
    else
      Border.BorderColor := clBlack;
  pbBorderTop.Repaint;
end;

procedure TfmQExport4ODTEditor.cbUseBorderClick(Sender: TObject);
begin
  if not Assigned(Border) then Exit;
  if cbUseBorder.Checked then
    Border.BorderStyle := bsODFSolid
  else
    Border.BorderStyle := bsODFNone;
  TuneBorderControls;
end;


procedure TfmQExport4ODTEditor.edBorderWidthChange(Sender: TObject);
begin
  if not Assigned(Border) then Exit;
  if StrToIntDef(edBorderWidth.Text, 0) <> Border.BorderWidth then
    Border.BorderWidth := StrToIntDef(edBorderWidth.Text, 0);
end;

procedure TfmQExport4ODTEditor.edBorderWidthEnter(Sender: TObject);
begin
  if not Assigned(Border) then Exit;
  if StrToIntDef(edBorderWidth.Text, 0) <> Border.BorderWidth then
    edBorderWidth.Text := IntToStr(Border.BorderWidth)
end;

procedure TfmQExport4ODTEditor.edBorderWidthExit(Sender: TObject);
begin
  if not Assigned(Border) then Exit;
  if StrToIntDef(edBorderWidth.Text, 0) <> Border.BorderWidth then
    edBorderWidth.Text := IntToStr(Border.BorderWidth)
end;

procedure TfmQExport4ODTEditor.ReplaceHeaderFooter;
var
  I: Integer;
begin
  ExportField.Header.Clear;
  for I := 0 to mmHeader.Lines.Count - 1 do
    ExportField.Header.Add(mmHeader.Lines[I]);
  ExportField.Footer.Clear;
  for I := 0 to mmFooter.Lines.Count - 1 do
    ExportField.Footer.Add(mmFooter.Lines[I]);
end;

procedure TfmQExport4ODTEditor.TuneSStyleButtons;
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    bRemove.Enabled := (lvSStyles.ItemFocused.Index >= 0);
    bMoveUp.Enabled := (lvSStyles.ItemFocused.Index > 0);
    bMoveDown.Enabled := (lvSStyles.ItemFocused.Index < lvSStyles.Items.Count - 1)
                         and (lvSStyles.ItemFocused.Index >= 0);
  end else
  begin
    bRemove.Enabled := (lvSStyles.Items.Count > 0);
    bMoveUp.Enabled := False;
    bMoveDown.Enabled := False;
  end;
end;

procedure TfmQExport4ODTEditor.bAddClick(Sender: TObject);
var
  Num: Integer;
begin
  SStylesList.Add;
  try
    Num := lvSStyles.Items.Count;
    with lvSStyles.Items.Add do
    begin
      Caption := {$IFDEF WIN32}QExportLoadStr(QED_ODT_StripStyle){$ENDIF}
        {$IFDEF LINUX}QED_ODT_StripStyle{$ENDIF} + IntToStr(Num + 1);
      ImageIndex := 1;
    end;
    lvSStyles.Items[lvSStyles.Items.Count - 1].Focused := true;
    lvSStyles.Items[lvSStyles.Items.Count - 1].Selected := true;
  finally
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4ODTEditor.lvSStylesChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  GetCurrentStyle;
  TuneSStyleButtons;
end;

procedure TfmQExport4ODTEditor.StyleRenumber(Item: TListItem);
begin
  Item.Caption := {$IFDEF WIN32}QExportLoadStr(QED_ODT_StripStyle){$ENDIF}
    {$IFDEF LINUX}QED_ODT_StripStyle{$ENDIF} + IntToStr(Item.Index + 1);
end;

procedure TfmQExport4ODTEditor.bRemoveClick(Sender: TObject);
begin
  if Assigned(lvSStyles.ItemFocused) then
  begin
    SStylesList.Items[lvSStyles.ItemFocused.Index].Free;
    lvSStyles.Items.BeginUpdate;
    try
      lvSStyles.Selected.Delete;
      ForAllListViewItems(lvSStyles, SetSelectedToFocused, false, true);
      ForAllListViewItems(lvSStyles, StyleRenumber, false, true);
    finally
      lvSStyles.Items.EndUpdate;
    end;
    TuneSStyleButtons;
  end;
end;

procedure TfmQExport4ODTEditor.SetSelectedToFocused(Item: TListItem);
begin
  if Item.Focused then
    Item.Selected := true;
end;

procedure TfmQExport4ODTEditor.FormOptions;
begin
  lvOptions.Items.BeginUpdate;
  try
    lvOptions.Items.Clear;
    AddListItem(lvOptions, {$IFDEF WIN32}QExportLoadStr(QED_ODT_Header){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Header{$ENDIF}, 2);
    AddListItem(lvOptions, {$IFDEF WIN32}QExportLoadStr(QED_ODT_CaptionRow){$ENDIF}
      {$IFDEF LINUX}QED_ODT_CaptionRow{$ENDIF}, 2);
    AddListItem(lvOptions, {$IFDEF WIN32}QExportLoadStr(QED_ODT_Data){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Data{$ENDIF}, 2);
    AddListItem(lvOptions, {$IFDEF WIN32}QExportLoadStr(QED_ODT_Footer){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Footer{$ENDIF}, 2);
    lvOptions.Items[0].Focused := true;
    lvOptions.Items[0].Selected := true;
  finally
    lvOptions.Items.EndUpdate;
  end;
end;

procedure TfmQExport4ODTEditor.AddListItem(Dest: TListView;
  ItemName: string; ImageInd: Integer);
begin
  with Dest.Items.Add do
  begin
    Caption := ItemName;
    ImageIndex := ImageInd;
  end;
end;

procedure TfmQExport4ODTEditor.FillStripStyles;
var
  I: Integer;
begin
  lvSStyles.Items.Clear;
  lvSStyles.Items.BeginUpdate;
  try
    for I := 0 to SStylesList.Count - 1 do
      AddListItem(lvSStyles, {$IFDEF WIN32}QExportLoadStr(QED_ODT_StripStyle){$ENDIF}
        {$IFDEF LINUX}QED_ODT_StripStyle{$ENDIF} + IntToStr(I + 1), 1);
    if assigned(lvSStyles.Items[0]) then
    begin
      lvSStyles.Items[0].Focused := true;
      lvSStyles.Items[0].Selected := true;
    end;
  finally
    lvSStyles.Items.EndUpdate;
  end;
end;

procedure TfmQExport4ODTEditor.bMoveUpClick(Sender: TObject);
var
  TempStyle: TODTCellParagraphStyle;
begin
  TempStyle := TODTCellParagraphStyle.Create(nil);
  try
    TempStyle.Assign(SStylesList.Items[lvSStyles.ItemFocused.Index]);
    SStylesList.Items[lvSStyles.ItemFocused.Index].Assign(SStylesList.Items[lvSStyles.ItemFocused.Index - 1]);
    SStylesList.Items[lvSStyles.ItemFocused.Index - 1].Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Selected := true;
  lvSStyles.Items[lvSStyles.ItemFocused.Index - 1].Focused := true;
end;

procedure TfmQExport4ODTEditor.bMoveDownClick(Sender: TObject);
var
  TempStyle: TODTCellParagraphStyle;
begin
  TempStyle := TODTCellParagraphStyle.Create(nil);
  try
    TempStyle.Assign(SStylesList.Items[lvSStyles.ItemFocused.Index]);
    SStylesList.Items[lvSStyles.ItemFocused.Index].Assign(SStylesList.Items[lvSStyles.ItemFocused.Index + 1]);
    SStylesList.Items[lvSStyles.ItemFocused.Index + 1].Assign(TempStyle);
  finally
    TempStyle.Free;
  end;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Selected := true;
  lvSStyles.Items[lvSStyles.ItemFocused.Index + 1].Focused := true;
end;

procedure TfmQExport4ODTEditor.BlockUnBlock(Condition: Boolean);
begin
  laTextFont.Enabled := Condition;
  cbTextFont.Enabled := Condition;
  laTextSize.Enabled := Condition;
  cbTextFontSize.Enabled := Condition;
  btnTextFontColor.Enabled := Condition;
  pbTextFontColor.Enabled := Condition;
  btnTextBold.Enabled := Condition;
  btnTextItalic.Enabled := Condition;
  btnTextStrikeOut.Enabled := Condition;
  btnTextUnderLine.Enabled := Condition;
  btnTextLeft.Enabled := Condition;
  btnTextCenter.Enabled := Condition;
  btnTextRight.Enabled := Condition;
  btnTextJustify.Enabled := Condition;
  cbTextBackground.Enabled := Condition;
  sbTextBackgroundColor.Enabled := Condition;
  pbBackColor.Enabled := Condition;
  cbTextUseHighlight.Enabled := Condition;
  pbTextHighlight.Enabled := Condition;
  sbTextHighlightColor.Enabled := Condition;
  laCellFont.Enabled := Condition;
  cbCellFont.Enabled := Condition;
  laCellFontSize.Enabled := Condition;
  cbCellFontSize.Enabled := Condition;
  btnCellColor.Enabled := Condition;
  pbCellFontColor.Enabled := Condition;
  btnCellBold.Enabled := Condition;
  btnCellItalic.Enabled := Condition;
  btnCellStrikeOut.Enabled := Condition;
  btnCellUnderlineSingle.Enabled := Condition;
  btnCellLeft.Enabled := Condition;
  btnCellCenter.Enabled := Condition;
  btnCellRight.Enabled := Condition;
  btnCellJustify.Enabled := Condition;
  btnCellVerTop.Enabled := Condition;
  btnCellVerCenter.Enabled := Condition;
  btnCellVerBottom.Enabled := Condition;
  cbCellUseBackground.Enabled := Condition;
  sbCellBackground.Enabled := Condition;
  pbCellBackground.Enabled := Condition;
  cbCellUseHighlight.Enabled := Condition;
  sbCellHighlight.Enabled := Condition;
  pbCellHighlight.Enabled := Condition;
end;

procedure TfmQExport4ODTEditor.GetCurrentStyle;
begin
  FCurrCellStyle := nil;
  FCurrTextStyle := nil;
  FCurrState := -1;
  if pcStyleEditor.ActivePage = tsOptions then
  begin
    if (not Assigned(lvOptions.ItemFocused)) or
      (not Assigned(lvOptions.Items[lvOptions.ItemFocused.Index])) then
    begin
      SetCurrentStatus;
      Exit;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      {$IFDEF WIN32}QExportLoadStr(QED_ODT_Header){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Header{$ENDIF} then
    begin
      FCurrTextStyle := HrStyle;
      FCurrState := 0;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      {$IFDEF WIN32}QExportLoadStr(QED_ODT_Footer){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Footer{$ENDIF} then
    begin
      FCurrTextStyle := FrStyle;
      FCurrState := 0;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
    {$IFDEF WIN32}QExportLoadStr(QED_ODT_CaptionRow){$ENDIF}
    {$IFDEF LINUX}QED_ODT_CaptionRow{$ENDIF} then
    begin
      FCurrCellStyle := CRStyle;
      FCurrState := 1;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      {$IFDEF WIN32}QExportLoadStr(QED_ODT_Data){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Data{$ENDIF} then
    begin
      FCurrCellStyle := DStyle;
      FCurrState := 1;
    end;
  end;
  if pcStyleEditor.ActivePage = tsStyles then
  begin
    if (not Assigned(lvSStyles.ItemFocused)) or
      (not Assigned(lvSStyles.Items[lvSStyles.ItemFocused.Index])) then
    begin
      SetCurrentStatus;
      Exit;
    end;
    FCurrState := 1;
    if lvSStyles.ItemFocused.Index < SStylesList.Count then
      FCurrCellStyle := SStylesList[lvSStyles.ItemFocused.Index];
  end;
  SetCurrentStatus;
end;

procedure TfmQExport4ODTEditor.SetCurrentStatus;
begin
  if FCurrState >= 0 then
  begin
    if FCurrState = 0 then
      pcAllOptions.ActivePage := tsCommonTextStyle
    else
      pcAllOptions.ActivePage := tsCommonCellStyle;
    BlockUnBlock(true);
    TuneOptionsList;
  end
  else
  begin
    BlockUnBlock(false);
    pbSampleText.Repaint;
  end;
end;

procedure TfmQExport4ODTEditor.TuneOptionsList;
begin
  if not (csDestroying in ComponentState) then
  begin
    if FCurrState = 0 then
    begin
      if not assigned(FCurrTextStyle) then Exit;
      cbTextFont.ItemIndex := cbTextFont.Items.IndexOf(FCurrTextStyle.Font.Name);
      cbTextFontSize.Text := IntToStr(FCurrTextStyle.Font.Size);
      TuneBackgroundControls(FCurrTextStyle.AllowBackground);
      TuneHighlightControls(FCurrTextStyle.AllowHighlight);
      btnTextLeft.Down := FCurrTextStyle.Alignment = taODFLeft;
      btnTextRight.Down := FCurrTextStyle.Alignment = taODFRight;
      btnTextCenter.Down := FCurrTextStyle.Alignment = taODFCenter;
      btnTextJustify.Down := FCurrTextStyle.Alignment = taODFJustify;
      btnTextBold.Down := fsBold in FCurrTextStyle.Font.Style;
      btnTextItalic.Down := fsItalic in FCurrTextStyle.Font.Style;
      btnTextUnderLine.Down := fsUnderline in FCurrTextStyle.Font.Style;
      btnTextStrikeOut.Down := fsStrikeOut in FCurrTextStyle.Font.Style;
      pbTextFontColor.Repaint;
      pbSampleText.Repaint;
    end;
    if FCurrState = 1 then
    begin
      if not assigned(FCurrCellStyle) then Exit;
      cbCellFont.ItemIndex := cbCellFont.Items.IndexOf(FCurrCellStyle.Font.Name);
      cbCellFontSize.Text := IntToStr(FCurrCellStyle.Font.Size);
      TuneBackgroundControls(FCurrCellStyle.AllowBackground);
      TuneHighlightControls(FCurrCellStyle.AllowHighlight);
      btnCellLeft.Down := FCurrCellStyle.Alignment = taODFLeft;
      btnCellRight.Down := FCurrCellStyle.Alignment = taODFRight;
      btnCellCenter.Down := FCurrCellStyle.Alignment = taODFCenter;
      btnCellJustify.Down := FCurrCellStyle.Alignment = taODFJustify;
      btnCellVerTop.Down := FCurrCellStyle.VerticalAligment = taODFTop;
      btnCellVerCenter.Down := FCurrCellStyle.VerticalAligment = taODFMiddle;
      btnCellVerBottom.Down := FCurrCellStyle.VerticalAligment = taODFBottom;
      btnCellBold.Down := fsBold in FCurrCellStyle.Font.Style;
      btnCellItalic.Down := fsItalic in FCurrCellStyle.Font.Style;
      btnCellUnderlineSingle.Down := fsUnderline in FCurrCellStyle.Font.Style;
      btnCellStrikeOut.Down := fsStrikeOut in FCurrCellStyle.Font.Style;
      pbCellFontColor.Repaint;
      pbSampleText.Repaint;
    end;
  end;
end;

procedure TfmQExport4ODTEditor.lvOptionsChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
  GetCurrentStyle;
end;

procedure TfmQExport4ODTEditor.pcStyleEditorChange(Sender: TObject);
begin
  GetCurrentStyle;
  TuneBorderControls;
end;

procedure TfmQExport4ODTEditor.TuneBackgroundControls(Condition: Boolean);
begin
  if FCurrState = 0 then
  begin
    cbTextBackground.Checked := Condition;
    sbTextBackgroundColor.Enabled := Condition;
    pbBackColor.Enabled := Condition;
    if Condition then
      pbBackColor.Repaint;
  end;
  if FCurrState = 1 then
  begin
    cbCellUseBackground.Checked := Condition;
    sbCellBackground.Enabled := Condition;
    pbCellBackground.Enabled := Condition;
    if Condition then
      pbCellBackground.Repaint;
  end;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.TuneHighlightControls(Condition: Boolean);
begin
  if FCurrState = 0 then
  begin
    cbTextUseHighlight.Checked := Condition;
    sbTextHighlightColor.Enabled := Condition;
    pbTextHighlight.Enabled := Condition;
    if Condition then
      pbTextHighlight.Repaint;
  end;
  if FCurrState = 1 then
  begin
    cbCellUseHighlight.Checked := Condition;
    sbCellHighlight.Enabled := Condition;
    pbCellHighlight.Enabled := Condition;
    if Condition then
      pbCellHighlight.Repaint;
  end;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbCellUseBackgroundClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  FCurrCellStyle.AllowBackground := cbCellUseBackground.Checked;
  TuneBackgroundControls(cbCellUseBackground.Checked);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbCellUseHighlightClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  FCurrCellStyle.AllowHighlight:= cbCellUseHighlight.Checked;
  TuneHighlightControls(cbCellUseHighlight.Checked);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbCellBackgroundClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if ColorDialog.Execute then
    if FCurrCellStyle.AllowBackground then
      if ColorDialog.Color <> -1 then
        FCurrCellStyle.BackgroundColor := ColorDialog.Color
      else
        FCurrCellStyle.BackgroundColor := clWhite;
  pbCellBackground.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbCellBackgroundMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbCellBackground);
end;

procedure TfmQExport4ODTEditor.sbCellBackgroundMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbCellBackground);
end;

procedure TfmQExport4ODTEditor.pbCellBackgroundPaint(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if not FCurrCellStyle.AllowBackground then Exit;
  PaintStandardColors(pbCellBackground, FCurrCellStyle.BackgroundColor);
end;

procedure TfmQExport4ODTEditor.sbCellHighlightClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if ColorDialog.Execute then
    if FCurrCellStyle.AllowHighlight then
      if ColorDialog.Color <> -1 then
        FCurrCellStyle.HighlightColor := ColorDialog.Color
      else
        FCurrCellStyle.HighlightColor := clWhite;
  pbCellHighlight.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbCellHighlightMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbCellHighlight);
end;

procedure TfmQExport4ODTEditor.sbCellHighlightMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbCellHighlight);
end;

procedure TfmQExport4ODTEditor.pbCellHighlightPaint(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if not FCurrCellStyle.AllowHighlight then Exit;
  PaintStandardColors(pbCellHighlight, FCurrCellStyle.HighlightColor);
end;

procedure TfmQExport4ODTEditor.cbTextBackgroundClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  FCurrTextStyle.AllowBackground := cbTextBackground.Checked;
  TuneBackgroundControls(cbTextBackground.Checked);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbTextBackgroundColorClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if ColorDialog.Execute then
    if FCurrTextStyle.AllowBackground then
      if ColorDialog.Color <> -1 then
        FCurrTextStyle.BackgroundColor := ColorDialog.Color
      else
        FCurrTextStyle.BackgroundColor := clWhite;
  pbBackColor.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbTextBackgroundColorMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  IncLeftAndTop(pbBackColor);
end;

procedure TfmQExport4ODTEditor.sbTextBackgroundColorMouseUp(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  DecLeftAndTop(pbBackColor);
end;

procedure TfmQExport4ODTEditor.pbBackColorPaint(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if not FCurrTextStyle.AllowBackground then Exit;
  PaintStandardColors(pbBackColor, FCurrTextStyle.BackgroundColor);
end;

procedure TfmQExport4ODTEditor.cbTextUseHighlightClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  FCurrTextStyle.AllowHighlight := cbTextUseHighlight.Checked;
  TuneHighlightControls(cbTextUseHighlight.Checked);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbTextHighlightColorClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if ColorDialog.Execute then
    if FCurrTextStyle.AllowHighlight then
      if ColorDialog.Color <> -1 then
        FCurrTextStyle.HighlightColor := ColorDialog.Color
      else
        FCurrTextStyle.HighlightColor := clWhite;
  pbTextHighlight.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.sbTextHighlightColorMouseDown(
  Sender: TObject; Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  IncLeftAndTop(pbTextHighlight);
end;

procedure TfmQExport4ODTEditor.sbTextHighlightColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbTextHighlight);
end;

procedure TfmQExport4ODTEditor.pbTextHighlightPaint(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if not FCurrTextStyle.AllowHighlight then Exit;
  PaintStandardColors(pbTextHighlight, FCurrTextStyle.HighlightColor);
end;

procedure TfmQExport4ODTEditor.btnCellColorClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if ColorDialog.Execute then
    if ColorDialog.Color <> -1 then
      FCurrCellStyle.Font.Color := ColorDialog.Color
    else
      FCurrCellStyle.Font.Color := clBlack;
  pbCellFontColor.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbCellFontColor);
end;

procedure TfmQExport4ODTEditor.btnCellColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbCellFontColor);
end;

procedure TfmQExport4ODTEditor.pbCellFontColorPaint(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  PaintStandardColors(pbCellFontColor, FCurrCellStyle.Font.Color);
end;

procedure TfmQExport4ODTEditor.btnTextFontColorClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if ColorDialog.Execute then
    if ColorDialog.Color <> -1 then
        FCurrTextStyle.Font.Color := ColorDialog.Color
      else
        FCurrTextStyle.Font.Color := clBlack;
  pbTextFontColor.Repaint;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextFontColorMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  IncLeftAndTop(pbTextFontColor);
end;

procedure TfmQExport4ODTEditor.btnTextFontColorMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  DecLeftAndTop(pbTextFontColor);
end;

procedure TfmQExport4ODTEditor.pbTextFontColorPaint(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  PaintStandardColors(pbTextFontColor, FCurrTextStyle.Font.Color);
end;

procedure TfmQExport4ODTEditor.btnTextBoldClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextBold.Down then
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style + [fsBold]
  else
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style - [fsBold];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextItalicClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextItalic.Down then
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style + [fsItalic]
  else
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style - [fsItalic];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextStrikeOutClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextStrikeOut.Down then
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style + [fsStrikeOut]
  else
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style - [fsStrikeOut];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextUnderLineClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextUnderLine.Down then
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style + [fsUnderline]
  else
    FCurrTextStyle.Font.Style := FCurrTextStyle.Font.Style - [fsUnderline];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellBoldClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellBold.Down then
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style + [fsBold]
  else
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style - [fsBold];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellItalicClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellItalic.Down then
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style + [fsItalic]
  else
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style - [fsItalic];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellStrikeOutClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellStrikeOut.Down then
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style + [fsStrikeOut]
  else
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style - [fsStrikeOut];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellUnderlineSingleClick(
  Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellUnderlineSingle.Down then
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style + [fsUnderline]
  else
    FCurrCellStyle.Font.Style := FCurrCellStyle.Font.Style - [fsUnderline];
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellLeftClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellLeft.Down then
    FCurrCellStyle.Alignment := taODFLeft;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellCenterClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellCenter.Down then
    FCurrCellStyle.Alignment := taODFCenter;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellRightClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellRight.Down then
    FCurrCellStyle.Alignment := taODFRight;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellJustifyClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellJustify.Down then
    FCurrCellStyle.Alignment := taODFJustify;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellVerTopClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellVerTop.Down then
    FCurrCellStyle.VerticalAligment := taODFTop;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellVerCenterClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellVerCenter.Down then
    FCurrCellStyle.VerticalAligment := taODFMiddle;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnCellVerBottomClick(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  if btnCellVerBottom.Down then
    FCurrCellStyle.VerticalAligment := taODFBottom;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextLeftClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextLeft.Down then
    FCurrTextStyle.Alignment := taODFLeft;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextCenterClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextCenter.Down then
    FCurrTextStyle.Alignment := taODFCenter;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextRightClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextRight.Down then
    FCurrTextStyle.Alignment := taODFRight;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.btnTextJustifyClick(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  if btnTextJustify.Down then
    FCurrTextStyle.Alignment := taODFJustify;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.TuneBorderControls;
var
  Condition: Boolean;
begin
  if not assigned(Border) then Exit;
  Condition := false;
  if Border.BorderStyle = bsODFSolid then
    Condition := true;
  laBorderColor.Enabled := Condition;
  btnBorderTopColor.Enabled := Condition;
  laBorderWidth.Enabled := Condition;
  edBorderWidth.Enabled := Condition;
  cbUseBorder.Checked := Condition;
  pbBorderTop.Enabled := Condition;
  edBorderWidth.Text := IntToStr(Border.BorderWidth);
  if Condition then
    pbBorderTop.Repaint;
end;

procedure TfmQExport4ODTEditor.NumberKeyPress(Sender: TObject;
  var Key: Char);
begin
  if not QExport4Common.CharInSet(Key, ['1', '2', '3', '4', '5', '6', '7',
                    '8', '9', '0', #8])
    then Key := #0;
end;

procedure TfmQExport4ODTEditor.pbSampleTextPaint(Sender: TObject);
begin
  if (FCurrTextStyle = nil) and (FCurrCellStyle = nil) then Exit;
  if FCurrCellStyle <> nil then
    DrawODTCellSample(pbSampleText, FCurrCellStyle);
  if FCurrTextStyle <> nil then
    DrawODTTextSample(pbSampleText, FCurrTextStyle);
end;

procedure TfmQExport4ODTEditor.cbTextFontChange(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  FCurrTextStyle.Font.Name := cbTextFont.Text;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbTextFontSizeChange(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  FCurrTextStyle.Font.Size := StrToIntDef(cbTextFontSize.Text, 10);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbTextFontSizeEnter(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  cbTextFontSize.Text := IntToStr(FCurrTextStyle.Font.Size);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbTextFontSizeExit(Sender: TObject);
begin
  if not Assigned(FCurrTextStyle) then Exit;
  cbTextFontSize.Text := IntToStr(FCurrTextStyle.Font.Size);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbCellFontChange(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  FCurrCellStyle.Font.Name := cbCellFont.Text;
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbCellFontSizeChange(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  FCurrCellStyle.Font.Size := StrToIntDef(cbCellFontSize.Text, 10);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbCellFontSizeEnter(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  cbCellFontSize.Text := IntToStr(FCurrCellStyle.Font.Size);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.cbCellFontSizeExit(Sender: TObject);
begin
  if not Assigned(FCurrCellStyle) then Exit;
  cbCellFontSize.Text := IntToStr(FCurrCellStyle.Font.Size);
  pbSampleText.Repaint;
end;

procedure TfmQExport4ODTEditor.bResetItemClick(Sender: TObject);
begin
  FCurrCellStyle := nil;
  FCurrTextStyle := nil;
  FCurrState := -1;
  if pcStyleEditor.ActivePage = tsOptions then
  begin
    if not assigned(lvOptions.Items[lvOptions.ItemFocused.Index]) then
      Exit;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      {$IFDEF WIN32}QExportLoadStr(QED_ODT_Header){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Header{$ENDIF} then
    begin
      HrStyle.SetDefault;
      FCurrTextStyle := HrStyle;
      FCurrState := 0;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      {$IFDEF WIN32}QExportLoadStr(QED_ODT_Footer){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Footer{$ENDIF} then
    begin
      FrStyle.SetDefault;
      FCurrTextStyle := FrStyle;
      FCurrState := 0;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
    {$IFDEF WIN32}QExportLoadStr(QED_ODT_CaptionRow){$ENDIF}
    {$IFDEF LINUX}QED_ODT_CaptionRow{$ENDIF} then
    begin
      CrStyle.SetDefault;
      FCurrCellStyle := CRStyle;
      FCurrState := 1;
    end;
    if lvOptions.Items[lvOptions.ItemFocused.Index].Caption =
      {$IFDEF WIN32}QExportLoadStr(QED_ODT_Data){$ENDIF}
      {$IFDEF LINUX}QED_ODT_Data{$ENDIF} then
    begin
      DStyle.SetDefault;
      FCurrCellStyle := DStyle;
      FCurrState := 1;
    end;
  end;
  if pcStyleEditor.ActivePage = tsStyles then
  begin
    if not assigned(lvSStyles.Items[lvSStyles.ItemFocused.Index]) then
      Exit;
    FCurrState := 1;
    SStylesList[lvSStyles.ItemFocused.Index].SetDefault;
    FCurrCellStyle := SStylesList[lvSStyles.ItemFocused.Index];
  end;
  SetCurrentStatus;
end;

procedure TfmQExport4ODTEditor.bResetAllClick(Sender: TObject);
var
  i: Integer;
begin
  if pcStyleEditor.ActivePage = tsOptions then
  begin
    HrStyle.SetDefault;
    FrStyle.SetDefault;
    CrStyle.SetDefault;
    DStyle.SetDefault;
  end;
  if pcStyleEditor.ActivePage = tsStyles then
    for i := 0 to SStylesList.Count - 1 do
      SStylesList[i].SetDefault;
  GetCurrentStyle;
end;

end.



